package com.easyaccount.service;

import cn.hutool.core.date.DateUtil;
import com.easyaccount.bean.BillTO;
import com.easyaccount.bean.BillVO;
import com.easyaccount.bean.LoginUser;
import com.easyaccount.commons.Constant;
import com.easyaccount.commons.TablePage;
import com.easyaccount.dao.BillMapper;
import com.easyaccount.dao.DailyMapper;
import com.easyaccount.dao.UserRoleMapper;
import com.easyaccount.model.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class BillInfoService {

    @Resource
    private BusibessInfoService busibessInfoService;
    @Resource
    private TrafficService trafficService;
    @Resource
    private HotelService hotelService;
    @Resource
    private OtherService otherService;
    @Resource
    private BillImgService billImgService;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private DailyMapper dailyMapper;
    @Resource
    private BillMapper billMapper;

    // 获取已提交
    public TablePage getCommitBillPage(TablePage tablePage,  HttpServletRequest request){
        int page = tablePage.getPage();
        int limit = tablePage.getLimit();
        int start = (page-1)*limit;

        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        List<BillTO> billTOs = billMapper.selectPageByUserId(start, limit, user.getId());
        int count = billMapper.selectPageByUserIdCount(user.getId());
        String contextPath = request.getContextPath();
        return getPage(billTOs,tablePage,count,contextPath);
    }

    // 获取未审核
    public TablePage getUnCheckBillPage(TablePage tablePage,  HttpServletRequest request){
        int page = tablePage.getPage();
        int limit = tablePage.getLimit();
        int start = (page-1)*limit;

        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        List<BillTO> billTOs = billMapper.selectPageByProgress(start, limit, getProgress(user.getId()));
        int count = billMapper.selectPageByProgressCount(getProgress(user.getId()));
        String contextPath = request.getContextPath();
        return getPage(billTOs,tablePage,count, contextPath);
    }

    // 获取已审核
    public TablePage getCheckedBillPage(TablePage tablePage,  HttpServletRequest request){
        int page = tablePage.getPage();
        int limit = tablePage.getLimit();
        int start = (page-1)*limit;

        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        List<BillTO> billTOs = billMapper.selectPageByProgress(start, limit, getCheckedProgress(user.getId()));
        int count = billMapper.selectPageByProgressCount(getProgress(user.getId()));
        String contextPath = request.getContextPath();
        return getPage(billTOs,tablePage,count, contextPath);
    }



    private TablePage getPage(List<BillTO> billTOs, TablePage tablePage, int count, String contextPath){
        List<BillVO> billVOS = new ArrayList<>();
        if(billTOs != null && billTOs.size() > 0){
            for (BillTO billTO : billTOs) {
                BillVO billVO = new BillVO();
                List<String> imgUrls = new ArrayList<>();
                List<BillImg> imgs = billImgService.getImgs(billTO.getCode());
                for (BillImg img : imgs) {
                    imgUrls.add(Constant.UPLOAD_FILE_URL + img.getImgName());
                }
                billTO.setImgs(imgUrls);
                BeanUtils.copyProperties(billTO,billVO);
                billVO.setCategory(parseCategory(billTO.getCategory()));
                billVO.setCreateTime(DateUtil.formatDate(billTO.getCreateTime()));
                billVO.setProgress(parseProgress(billTO.getProgress()));
                billVOS.add(billVO);
            }
        }

        tablePage.setCount(count);
        tablePage.setData(billVOS);
        return tablePage;
    }

    private String parseProgress(Integer progress) {
        String result = "";
        switch (progress){
            case Constant.PROGRESS_COMMIT:
                result = "财务审核中";
                break;
            case Constant.PROGRESS_CW_CHECKED:
                result = "处长审核中";
                break;
            case Constant.PROGRESS_FINAL_CHECKED:
                result = "审核完成";
                break;
            case Constant.PROGRESS_REFUSED:
                result = "审核不通过";
                break;
        }

        return result;
    }

    private String parseCategory(Integer category) {
        String result = "";
        if(category == Constant.ACCOUNT_DAILY){
            result = "日常报销";
        }else if(category == Constant.ACCOUNT_BUSINESS){
            result = "国内差旅报销";
        }
        return result;
    }

    private int getProgress(int userId){
        UserRole userRole = userRoleMapper.selectByUserId(userId);
        if(userRole.getRoleId() == 2){
            return Constant.PROGRESS_CW_CHECKED;
        }else if(userRole.getRoleId() == 3){
            return Constant.PROGRESS_COMMIT;
        }else{
            return Constant.PROGRESS_COMMIT;
        }
    }

    private int getCheckedProgress(int userId){
        UserRole userRole = userRoleMapper.selectByUserId(userId);
        if(userRole.getRoleId() == 2){
            return Constant.PROGRESS_FINAL_CHECKED;
        }else if(userRole.getRoleId() == 3){
            return Constant.PROGRESS_CW_CHECKED;
        }
        return 0;
    }

}
